//
//  ViewController.m
//  添加图片
//
//  Created by MB__Lee on 2018/5/4.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "ViewController.h"

#import "MBCollectionView.h"

#import "MBColleCell.h"
#import "MBPickCell.h"

#define Width [UIScreen mainScreen].bounds.size.width
#define Height  [UIScreen mainScreen].bounds.size.height

#define length(a) Width/375.0 *a

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIImagePickerControllerDelegate,MBCollectionViewDelegate>

@property(nonatomic,strong)NSMutableArray *imageMUT;//用来存贮选择的照片
@property(nonatomic,strong)UICollectionView *collectionView;//


@property(nonatomic,strong)UIImagePickerController *picker;

@end


static NSString *MBColleCell_identi = @"MBColleCell";
static NSString *MBPickCell_identi = @"MBPickCell";
@implementation ViewController

-(NSMutableArray *)imageMUT{
    if (!_imageMUT) {
        _imageMUT  = [NSMutableArray array];
        UIImage *image = [UIImage imageNamed:@"hetong_tianjia_icon"];
        [_imageMUT addObject:image];
    }
    return  _imageMUT;
}
-(UIImagePickerController *)picker{
    if (!_picker) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        
        _picker = picker;
        
    }
    return  _picker;
}

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        

        UICollectionViewFlowLayout *layout  =[[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(length(110), length(110));
        layout.minimumInteritemSpacing = length(7);
        layout.minimumLineSpacing = length(7);
        
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, Width, Height) collectionViewLayout:layout];
        
        [collectionView registerNib:[UINib nibWithNibName:MBColleCell_identi bundle:nil] forCellWithReuseIdentifier:MBColleCell_identi];
        [collectionView registerNib:[UINib nibWithNibName:MBPickCell_identi bundle:nil] forCellWithReuseIdentifier:MBPickCell_identi];
        collectionView.backgroundColor = [UIColor whiteColor];
        
        collectionView.delegate = self;
        collectionView.dataSource = self;
        
        _collectionView = collectionView;
    }
    return  _collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1.添加layout
    UICollectionViewFlowLayout *layout  = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(length(110), length(110));
    layout.minimumInteritemSpacing = length(7);
    layout.minimumLineSpacing = length(7);
    
//    [self.view addSubview: self.collectionView];
    
    //2.赋值layout
    MBCollectionView *collection = [[MBCollectionView alloc] initWithFrame:CGRectMake(0, 0, Width, 400) collectionViewLayout:layout];
    //3.添加代理方法
    collection.MBDelegate = self;
    
    [self.view addSubview: collection];
    
}

#pragma mark --代理方法
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return  1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageMUT.count > 6 ? 6 : self.imageMUT.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MBColleCell *cell = nil;
    
  
        NSInteger count = self.imageMUT.count - 2;//用来判断用哪种cell 的临界值
        if (indexPath.row > count) {
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:MBPickCell_identi forIndexPath:indexPath];
        }else{
            cell  = [collectionView dequeueReusableCellWithReuseIdentifier:MBColleCell_identi forIndexPath:indexPath];
        }
        
    
    cell.showIMG = self.imageMUT[indexPath.row];
    __weak typeof(self) weakSelf = self;
    cell.deleteBlock = ^{
        [weakSelf.imageMUT removeObjectAtIndex:indexPath.row];
        [weakSelf.collectionView reloadData];
    };
    
    
    
    
    return  cell;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return  UIEdgeInsetsMake(5, 15, 15, 15);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row > self.imageMUT.count - 2 || (indexPath.row == 0 && self.imageMUT.count == 1)) {
        [self presentViewController:self.picker animated:YES completion:nil];
    }
}



#pragma mark --照片选择器
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
   
    UIImage *editImg = info[UIImagePickerControllerEditedImage];
    [self.imageMUT insertObject:editImg atIndex:0];
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    [self collectionReloadAndLayout];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
//collectionView 刷新，然后重新布局frame
-(void)collectionReloadAndLayout{
    self.collectionView.frame = [self caculateFrame];
    self.collectionView.backgroundColor = [UIColor grayColor];
    [self.collectionView reloadData];
    
}
//根据数组内容来计算collection 的frame
-(CGRect)caculateFrame{
    CGRect frame;//要返回的frame
    CGFloat height;// frame 的高度
    NSUInteger count = self.imageMUT.count;
    if (count < 4) {
        //110 item 大小，15 上边距，
        height = length(110) + length(15) + length(15);
        
    }else {
        height = 2 * length(110) + length(15) + length(15) + length(7);
    }
    frame = CGRectMake(0, 0, Width, height);
    return  frame;
}


#pragma mark --MBCollectionViewDelegate --内置有此代理方法无需实现
-(void)MBCollectionView:(MBCollectionView *)collectionview presentPickerView:(UIImagePickerController *)picker{
    [self presentViewController:picker animated:YES completion:nil];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
